<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class General extends MX_Controller
	{
		private $module = "general";
		
		public function __construct()
		{
			parent::__construct();
			$this->load->model($this->module.'_model','model');
			$this->load->library('pagination');
			
			$data['subcat'] = $this->model->getParentCat();
						
			$data['module'] = $this->module;
			
			$this->template->set_template('admin');
			$this->template->write('title','Shopping Cart');
			$this->template->write_view('menu','BACKEND/menu');
			$this->template->write_view('search','BACKEND/search',$data);
			$this->template->write_view('left','BACKEND/menu_left');
			$this->template->write_view('footer','BACKEND/footer');
		}
		
		public function admin_index($start = 0,$limit = 10)
		{
			$config['total_rows'] = $this->model->admin_count();
			$config['base_url'] = base_url().'admin/'.$this->module.'/index/';
			$config['per_page'] = $limit;
			$config['num_links'] = 5;
			$config['uri_segment'] = 4;
			
			$this->pagination->initialize($config); 
			
			$result = $this->model->admin_index($config['per_page'],$start);
			
			$data = array(
				'module'	 => $this->module,
				'result'	 => $result
			);
			
			$this->template->write_view('content','BACKEND/index',$data);
			$this->template->render();
		}
		
		public function admin_news($start = 0,$limit = 10)
		{
			$config['total_rows'] = $this->model->admin_count();
			$config['base_url'] = base_url().'admin/'.$this->module.'/index/';
			$config['per_page'] = $limit;
			$config['num_links'] = 5;
			$config['uri_segment'] = 4;
			
			$this->pagination->initialize($config); 
			
			$result = $this->model->admin_news($config['per_page'],$start);
			
			$data = array(
				'module'	 => $this->module,
				'result'	 => $result
			);
			
			$this->template->write_view('content','BACKEND/index',$data);
			$this->template->render();
		}
		
		
		public function admin_save()
		{
			if($_POST)
			{		
				$data = array();
				$file_name = '';
				$file_size = '';
				$file_tmp = '';
				$file_type = '';
				$file_error = '';
				$file_size_accept = 1024;
				$array_file_type = array('jpg','png');
				
				if($_FILES['image_thumb']['error']==0){
						
					$image_thumb = $_FILES['image_thumb'];
					
					$path_info = pathinfo($image_thumb['name']);
					$extension = strtolower($path_info['extension']);
					if($extension == 'jpg' || $extension == 'png'){
						$file_name = 'hts_'.md5(uniqid(mt_rand())).'.'.$extension;
						$file_size = $image_thumb['size'];
						$file_tmp = $image_thumb['tmp_name'];
					}else{
						header('Location: '.base_url().'admin/'.$this->module.'/index/'.$id);
						exit;
					}
				}
			
					//VAR FOLDER				
				if(!file_exists(IMAGE_LARGE_DIR)) {
					@mkdir(IMAGE_LARGE_DIR, 0777);
				}
				
				if(!file_exists(IMAGE_THUMB_DIR)) {
					@mkdir(IMAGE_THUMB_DIR, 0777);
				}
				
				if($this->model->admin_save($file_name))
				{
					if($_FILES['image_thumb']['error']==0){
						move_uploaded_file($file_tmp,IMAGE_LARGE_DIR.$file_name);
						@resize_image(IMAGE_LARGE_DIR,$file_name, 120, 120, IMAGE_THUMB_DIR);
					}
				}
			}
			header('Location: '.base_url().'admin/'.$this->module.'/');
		}
		
		public function admin_sort()
		{
			$data = array();
			$sort   = $this->input->post('value');
			$result = $this->model->admin_sort($sort);
			
			if(!empty($result))
			{
				$data = array(
					'module' => $this->module,
					'result' => $result
				);
				$this->load->view('BACKEND/ajax_index',$data);
			}else{
				$data = array(
					'module' => $this->module,
					'result' => ''
				);
				$this->load->view('BACKEND/ajax_index',$data);
			}
		}
		
		public function admin_update($id = 0)
		{
			$result[0] = array();
			if($id != 0)
			{
				$result = $this->model->admin_update($id);
			}
			$parent = $this->model->_getParentMenu();
			$sub	= $this->model->getSubMenu();
			
			$data = array(
					'result'   	 	=> $result[0],
					'module'   	 	=> $this->module,
					'parent_menu'	=> $parent,
					'sub'			=> $sub
			);
			
			$this->template->write_view('content','BACKEND/update',$data);
			$this->template->render();
		}
		
		public function admin_del($id = 0)
		{
			$id = intval($id);
			$this->model->admin_del($id);
			header('location: '.base_url().'admin/'.$this->module.'/');
		}
		
		public function admin_update_status()
		{
			$id = $this->input->post('id');
			if($this->input->post('status') == 1)
			{
				$status = 0;
			}else{
				$status = 1;
			}
			
			$this->model->admin_update_status($id,$status);
			
			$data_update = array(
				'id'=>$id,
				'status'=>$status
			);
			
			$this->load->view('BACKEND/ajax_status',$data_update);
		}
		
		public function admin_del_all()
		{
			if($_POST)
			{
				$this->model->admin_del_all();
				header('location: '.base_url().'admin/'.$this->module.'/');
			}else{
				header('location: '.base_url().'admin/'.$this->module.'/');
			}
		}
		
		public function admin_search()
		{
			$data = array();
			if($_POST)
			{
				$result = $this->model->admin_search();
				$data = array(
					'module' => $this->module,
					'result' => $result
				);
				$this->template->write_view('content','BACKEND/index',$data);
				$this->template->render();
			}else{
				header('Location: '.base_url().'admin/'.$this->module.'/');
			}
		}
			
		// Loc du lieu theo category
		public function admin_getSubCat($id = 0)
		{
			$id = (int)$id;
			$result = $this->model->getSubCat($id);
			$data = array(
				'result' => $result,
				'module' => $this->module
			);
			$this->template->write_view('content','BACKEND/index',$data);
			$this->template->render();
		}
		
		public function index()
		{			
			$result = $this->model->get_news(5,5,0);
			
			$this->_fillChildMenuList($result);
			
			$images_library = $this->model->get_images_library();
					
			$data = array('result' => $result,'images_library'=>$images_library);
			
			$this->template->set_template('default');
			$this->template->write('title','NO-DATA');
			$this->template->write_view('menu','FRONTEND/menu');
			$this->template->write_view('logo','FRONTEND/logo');
			$this->template->write_view('content','FRONTEND/content',$data);
			$this->template->render();
		}

		function _fillChildMenuList(&$parent_list)
		{
			if(!empty($parent_list))
			{
				for($i=0;$i<count($parent_list);$i++)
				{
					$news = $this->model->getNews($parent_list[$i]['id']);
					$parent_list[$i]['news_detail'] = $news; 	
					if(!empty($news))
					{
						$parent_list[$i]['related_list'] = $this->model->_getRelatedNews($news[0]['id'],$parent_list[$i]['id']);
					
					}
				}
			}
		}
		
		public function getContent($id = 0,$start = 0,$limit = 8)
		{
			$controller = $this->uri->segment(1);
			$function = $this->uri->segment(2);
			
			$config['total_rows'] = $this->model->count_content($id);
			$config['base_url'] = base_url().$controller.'/'.$function.'/';
			$config['per_page'] = $limit;
			$config['num_links'] = 5;
			$config['uri_segment'] = 3;
			
			$this->pagination->initialize($config); 

			$result = $this->model->getContent($id,$start,$config['per_page']);
			$module = $this->model->getModule($id);
			
			$data = array('result'=>$result,'module'=>$module);
			
			$this->template->set_template('default');
			$this->template->write('title','Live Concert');
			$this->template->write_view('menu','FRONTEND/menu');
			$this->template->write_view('logo','FRONTEND/logo');
			
			if($controller == 'cuoc-thi' || $controller == 'live-concert'){
				$this->template->write_view('content','FRONTEND/content_news',$data);
			}else{
				$this->template->write_view('content','FRONTEND/content_tintuc',$data);
			}
			$this->template->render();
		}
		
		public function getContentId($slug = '')
		{
			$controller = $this->uri->segment(1);
			$function = $this->uri->segment(2);
			
			$result = $this->model->getContentId($slug);
			$relatedNews = $this->model->relatedNews($slug);
			
			$data = array('result'=>$result,'relatedNews'=>$relatedNews);
			
			$this->template->set_template('default');
			$this->template->write('title','Live Concert');
			$this->template->write_view('menu','FRONTEND/menu');
			$this->template->write_view('logo','FRONTEND/logo');
			
			$this->template->write_view('content','FRONTEND/news_detail',$data);
			$this->template->render();
		}
		
		public function specAllContent($parent = 0)
		{
			$result = $this->model->specAllContent($parent);
			$data = array('result'=>$result);
			
			$this->template->set_template('default');
			$this->template->write('title','Live Concert');
			$this->template->write_view('menu','FRONTEND/menu');
			$this->template->write_view('logo','FRONTEND/logo');
			$this->template->write_view('content','FRONTEND/gioithieu',$data);
			$this->template->render();
		}
		
		public function getAllContentNews($parent = 0,$start = 0,$limit = 5)
		{
			$controller = $this->uri->segment(1);
				
			$config['total_rows'] = $this->model->count($parent);
			$config['base_url'] = base_url().$controller.'/';
			$config['per_page'] = $limit;
			$config['num_links'] = 5;
			$config['uri_segment'] = 2;
			
			$this->pagination->initialize($config); 
			
			$parentContent = $this->model->getParentContent($parent,$start,$config['per_page']);
			
			$module = $this->model->getModule($parent);
			$data = array('result'=>$parentContent,'module'=>$module);
			
			$this->template->set_template('default');
			$this->template->write('title','Live Concert');
			$this->template->write_view('menu','FRONTEND/menu');
			$this->template->write_view('logo','FRONTEND/logo');
			$this->template->write_view('content','FRONTEND/content_tintuc',$data);
			$this->template->render();
		}
		
		public function getAllContent($parent = 0,$start = 0,$limit = 5)
		{
			$controller = $this->uri->segment(1);
				
			$config['total_rows'] = $this->model->count($parent);
			$config['base_url'] = base_url().$controller.'/';
			$config['per_page'] = $limit;
			$config['num_links'] = 5;
			$config['uri_segment'] = 2;
			
			$this->pagination->initialize($config); 
			
			$parentContent = $this->model->getParentContent($parent,$start,$config['per_page']);
			
			$module = $this->model->getModule($parent);
			$data = array('result'=>$parentContent,'module'=>$module);
			
			$this->template->set_template('default');
			$this->template->write('title','Live Concert');
			$this->template->write_view('menu','FRONTEND/menu');
			$this->template->write_view('logo','FRONTEND/logo');
			
			if($controller == 'live-concert' || $controller == 'cuoc-thi' || $controller == 'tin-tuc'){
				$this->template->write_view('content','FRONTEND/content_news',$data);
			}else{
				$this->template->write_view('content','FRONTEND/content',$data);
			}
			$this->template->render();
		}
		
		function _fillContent(&$parent_list)
		{
			if(!empty($parent_list))
			{
				for($i=0;$i<count($parent_list);$i++)
				{
					$parent_list[$i]['news_detail'] = $this->model->getAllContent($parent_list[$i]['id']);	
				}
			}
		}
		
		public function gethinhanh($start = 0,$limit = 6 )
		{
			$config['total_rows'] = $this->model->count_hinhanh();
			$config['base_url'] = base_url().'hinh-anh/';
			$config['per_page'] = $limit;
			$config['num_links'] = 5;
			$config['uri_segment'] = 2;
			
			$this->pagination->initialize($config); 
			
			$result = $this->model->gethinhanh($start,$config['per_page']);
			$module = 'HÌNH ẢNH';
			$data = array('result'=>$result,'module'=>$module);
			
			$this->template->set_template('default');
			$this->template->write('title','Hình ảnh');
			$this->template->write_view('menu','FRONTEND/menu');
			$this->template->write_view('logo','FRONTEND/logo');
		
			$this->template->write_view('content','FRONTEND/hinhanh',$data);
			
			$this->template->render();
		}
		
		public function getHinhAnhDetail($id = 0,$start = 0,$limit = 6)
		{
			
			$config['total_rows'] = $this->model->count_hinhanh_detail($id);
			$config['base_url'] = base_url().'hinh-anh/catalog/'.$id.'/';
			$config['per_page'] = $limit;
			$config['num_links'] = 5;
			$config['uri_segment'] = 4;
			
			$this->pagination->initialize($config); 
			
			$result = $this->model->getHinhAnhDetail($id,$start,$config['per_page']);
			
			$module = 'HÌNH ẢNH';
			$data = array('result'=>$result,'module'=>$module);
		
			$this->template->set_template('default');
			$this->template->write('title','Hình ảnh');
			$this->template->write_view('menu','FRONTEND/menu');
			$this->template->write_view('logo','FRONTEND/logo');
		
			$this->template->write_view('content','FRONTEND/hinhanh_catalog',$data);
			
			$this->template->render();
		}
		
		public function hinhanhid($id = 0)
		{
			$result = $this->model->hinhanhid($id);
			
			$module = 'HÌNH ẢNH';
			$data = array('result'=>$result,'module'=>$module);
			
			$this->template->set_template('default');
			$this->template->write('title','Hình ảnh');
			$this->template->write_view('menu','FRONTEND/menu');
			$this->template->write_view('logo','FRONTEND/logo');
		
			$this->template->write_view('content','FRONTEND/hinhanh_catalog_detail',$data);
			
			$this->template->render();
		}
		
		public function lienhe()
		{
			$data = array();
			$this->template->set_template('default');
			$this->template->write('title','Liên hệ');
			$this->template->write_view('menu','FRONTEND/menu');
			$this->template->write_view('logo','FRONTEND/logo');
		
			$this->template->write_view('content','FRONTEND/lienhe',$data);
			
			$this->template->render();
		}
		
		public function postlienhe()
		{
			if($_POST){
				if($this->model->postlienhe())
				{
					header('Location: '.base_url().'lien-he');
				}
			}
		}
	}
?>